package telbook;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.cj.protocol.Resultset;

/**
 * Servlet implementation class SignServlet
 */
public class SignServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		String name=request.getParameter("username");
		String password=request.getParameter("password");
		String password2=request.getParameter("password2");
		String role=request.getParameter("role");
		String ID=request.getParameter("id");
		Cookie cookie=new Cookie("role", role);
		response.addCookie(cookie);
		boolean flag=false;
		int table=1;
		FormBean FB=new FormBean();
		FB.setUsername(name);
		FB.setPassword(password);
		FB.setPassword2(password2);
		if(!FB.validate()) {
			request.setAttribute("FB", FB);
			request.getRequestDispatcher("/signup.jsp").forward(request, response);
			return;
		}
		UserBean userbean=new UserBean();
		userbean.setName(name);
		userbean.setPassword(password);
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try {
//			获取数据连接
			conn=JDBCUtils.getConnection();
//			获取Statement对象
			stmt=conn.createStatement();
//			发送SQL语句
			String sql="select password from "+role+" where name='"+name+"'";
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				request.setAttribute("DBMes", "您注册的用户已存在");
				request.getRequestDispatcher("/signup.jsp")
				.forward(request, response);
				}
			else {
				while(!flag) {
					if(role.equals("users")) {
						try {
							
							sql="create table user"+table+"(name varchar(40),"
									+ "number varchar(40),"
									+ "address varchar(60),"
									+ "qq varchar(10),primary key(number))"
									+ "character set utf8 collate utf8_general_ci";
							stmt.execute(sql);
							flag=true;
							table++;
							
						}catch(Exception e){
							table++;
						}
					}
					else {
						break;
					}
				}
				if(role.equals("users")) {
					sql="insert into "+role+" value('"+name+"','"+password2+"','user"+(table-1)+"')";
					stmt.execute(sql);
				}
				else if(ID.equals("8045")&&role.equals("admin")){
					sql="insert into "+role+" value('"+name+"','"+password2+"')";
					stmt.execute(sql);
				}
				else {
					request.setAttribute("DBMs", "ID错误");
					request.getRequestDispatcher("/signup.jsp")
					.forward(request, response);
				}
				response.getWriter().print("注册成功，三秒后自动登录");
				cookie=new Cookie("userbean", name);
				response.addCookie(cookie);
				if(role.equals("users"))
					response.setHeader("refresh", "3;url=index.jsp");
				else
					response.setHeader("refresh", "3;url=index2.jsp");
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.release(rs, stmt, conn);
		}
	}

}
